package com.ljc.utils;

import com.ljc.model.entity.InterfaceInfo;
import com.ljc.service.InterfaceInfoService;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class RedissonBloomFilterUtils {
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private InterfaceInfoService interfaceInfoService;
    private RBloomFilter<Long> bloomFilter;

    @PostConstruct
    public void initInterfaceIntoBloomFilter() {
        bloomFilter = redissonClient.getBloomFilter("myBloomFilter");
        bloomFilter.tryInit(100, 0.001);
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.list();
        for (InterfaceInfo interfaceInfo : interfaceInfoList) {
            bloomFilter.add(interfaceInfo.getId());
        }
    }

    public boolean isContain(Long id) {
        return bloomFilter.contains(id);
    }

    public void addInterfaceToBloomFilter(Long id) {
        bloomFilter.add(id);
    }
}
